// pages/ PublishProblems/index.js
import {
  HTTP
} from '../../../utils/http.js'
let Myhttp = new HTTP()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    problem:{
      qid:'',
      name:'',
      type:'1',
      answer:[]
    },
    problemId:'',
    Letter:['A','B','C','D','E'],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.qid){
      this.setData({
        'problem.qid': options.qid
      })
    } 
    if (options.id){
      this.setData({
        'problemId': options.id
      })
      Myhttp.request({
        url: '/questionnaire/questionnaire/editProblemShow',
        data: {
          pid: this.data.problemId
        },
        success: (res) => {
          let data = res.data.data
          let list = data.answer_list
          list.forEach((val, index) => {
            val.title = this.data.Letter[index]
          })
          this.setData({
            'problem.name': data.problem_info.name,
            'problem.type': data.problem_info.type,
            'problem.answer': list
          })
        }

      })
    }

    let datas = this.data.problem.answer
    if(this.data.problem.answer.length<2){
      for (let i = 0; i < 2; i++) {
        let aa = { title: this.data.Letter[i], name: '' }
        datas.push(aa)
      }
      this.setData({
        'problem.answer': datas
      })
    }
    
    //console.log(options.qid)
  },
  radioChange:function(e){
    let value = e.detail.value
    this.setData({
      'problem.type': value
    })
    // console.log(e.detail)
    // console.log(value)
  },
  addSelect:function(){
      
    let aa = this.data.problem.answer
    let a = ['A','B','C','D','E','F','G','H','I','J']
    let index = aa.length

    if (index < 10){
      // console.log('<4')
      aa.push({
        title: a[index],
        name: ''
      })
        this.setData({
          'problem.answer': aa
        })
    }else{
      wx.showToast({
        title:'选项上限十个,超出无效!'
      })
    }
    

    // console.log(aa)
  },
  inputclick:function(e){
    let that = this
    let index = e.target.dataset.index
    // "sDate[" + index + "].selected"
    let data = "problem.answer[" +index+ "].name"
    let value = e.detail.value
    that.setData({
      [data]: value
    })
  },
  titleclick:function(e){
    let value = e.detail.value
    this.setData({
      'problem.name': value
    })
    //console.log(e.detail.value)
  },
  Preservation: function () {
    //console.log('1')
      // console.log(this.data)
    let datas = this.data.problem
    datas.answer.forEach((val,index)=>{
      if(val.value == ''){
        datas.answer.splice(index,1)
      }
    })
    if (this.data.problemId){
      datas.pid = this.data.problemId
    }
    

    Myhttp.request({
      url: '/questionnaire/questionnaire/editProblem',
      data: datas,
      success: (res) => {
        if(res.data.code == 200){
          wx.navigateBack({
            delta:1
          })
        }else{
          wx.showToast({
            title:res.data.msg,
            icon:'none',
          })
        }
        
      }

    })
    
  },
  delete:function(e){
    
    //console.log(e.target.dataset.index)
    let index = e.target.dataset.index
    let datas = this.data.problem.answer
    if (datas.length > 2){
      datas.splice(index, 1)
      for (let i = 0; i < datas.length; i++) {
        datas[i].title = this.data.Letter[i]
      }
      this.setData({
        'problem.answer': datas
      })
    } 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/PublishQuestionnaire/PublishProblems/index'
    }
  }
})